//
//  SLRemindView.m
//  SLApp
//
//  Created by 刘洋 on 15/7/22.
//  Copyright (c) 2015年 Qiniu. All rights reserved.
//

#import "SLRemindView.h"
#import "JFKitPri.h"

#define kHiddenDelayTime 2

@implementation SLRemindView

#pragma mark 显示信息
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view {
    if (text.length == 0) {
        return;
    }
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    dispatch_async(dispatch_get_main_queue(), ^{
        // 快速显示一个提示信息
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.detailsLabel.textColor = [UIColor colorWithHexString:@"#ffffff"];
        hud.contentColor = [UIColor colorWithHexString:@"#ffffff"];
        hud.mode = MBProgressHUDModeText;
        hud.offset = CGPointMake(0, -50);
        
        // 设置图片
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"%@", icon]]];
        // 再设置模式
        hud.mode = MBProgressHUDModeCustomView;
        
       /* NSString * textTemp = [NSString stringWithFormat:@" %@",text];
        //2.初始化富文本对象
        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:textTemp];
        
        //2.1修改富文本中的不同文字的样式
        [attributedString addAttribute:NSForegroundColorAttributeName value:[UIColor whiteColor] range:NSMakeRange(0, textTemp.length)];//字体颜色
        [attributedString addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, textTemp.length)];//字体大小
        NSTextAttachment * attach = [[NSTextAttachment alloc] init];
        attach.image = [UIImage imageNamed:icon];
        attach.bounds = CGRectMake(0, -4, 19, 19);
        
        //4.创建带有图片的富文本
        NSAttributedString *string = [NSAttributedString attributedStringWithAttachment:(NSTextAttachment *)(attach)];
        [attributedString insertAttributedString:string atIndex:0];//插入
        hud.detailsLabel.attributedText = attributedString;
        */
        hud.detailsLabel.text = text;
        hud.margin = 10;
     
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = HYHudColor;
        // 隐藏时候从父控件中移除
      
        hud.removeFromSuperViewOnHide = YES;
        hud.userInteractionEnabled = NO;
        
        // 1秒之后再消失
        [hud hideAnimated:YES afterDelay:kHiddenDelayTime];
    });
}

#pragma mark 显示错误信息
+ (void)showError:(NSString *)error toView:(UIView *)view {
    [self show:error icon:@"fail" view:view];
}

+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    [self show:success icon:@"success" view:view];
}

#pragma mark 显示一些信息
+ (void)showMessage:(NSString *)message toView:(UIView *)view
{
    if (message.length == 0) {
        return;
    }
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    dispatch_async(dispatch_get_main_queue(), ^{
        // 快速显示一个提示信息
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
        hud.detailsLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:14];
        hud.detailsLabel.text = message;
        hud.contentColor = [UIColor colorWithHexString:@"#ffffff"];
        hud.mode = MBProgressHUDModeText;
        hud.offset = CGPointMake(0, -50);
        // 隐藏时候从父控件中移除
        hud.removeFromSuperViewOnHide = YES;
        hud.userInteractionEnabled = NO;
        //    // YES代表需要蒙版效果
        //    hud.dimBackground = YES;
        hud.margin = 10;
        hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
        hud.bezelView.backgroundColor = HYHudColor;
        [hud hideAnimated:YES afterDelay:kHiddenDelayTime];
    });
}

//添加一个转菊花
+(void)showHUDAddedTo:(UIView *)view animated:(BOOL)animated
{

    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:animated];
    hud.mode = MBProgressHUDModeIndeterminate;
    hud.contentColor = [UIColor colorWithHexString:@"#ffffff"];
    hud.removeFromSuperViewOnHide = YES;
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = HYHudColor;
    [hud hideAnimated:YES afterDelay:30];
     hud.margin = 10;
    [view addSubview:hud];
    [hud showAnimated:animated];
}

+(void)hideHUDForView:(UIView *)view animated:(BOOL)animated
{
    if (view == nil) view = [UIApplication sharedApplication].keyWindow;
    dispatch_async(dispatch_get_main_queue(), ^{
        [MBProgressHUD hideHUDForView:view animated:YES];
    });
}

+(void)hideAllHudsForView
{
    dispatch_async(dispatch_get_main_queue(), ^{
        UIView * windowView = [UIApplication sharedApplication].keyWindow;
        [MBProgressHUD hideHUDForView:windowView animated:YES];
    });
}




@end
